<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="../../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../../css/normalize.css"/>
    <link rel="stylesheet" href="../../../css/fa.css"/>
    <link rel="stylesheet" href="../../../css/icon.css"/>
    <link rel="stylesheet" href="../../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../../css/app.min.css"/>
    <link rel="stylesheet" href="../../../css/iframe.css"/>
    <style>
        .layui-form-checkbox, .layui-form-checkbox > span {
            width: 100%;
        }
    </style>
</head>

<body>
<form class="ares-p layui-form" lay-filter="form">
    <div class="layui-form-item">
        <label class="layui-form-label">套餐</label>
        <div class="layui-input-block">
            <select id="combo" name="combo" lay-verify="required" lay-filter="combo"></select>
        </div>
    </div>
    <!--<div class="layui-form-item">
        <label class="layui-form-label">治疗人员</label>
        <div class="layui-input-block">
            <select id="treatment-therapist" name="treatmentTherapist" lay-verify="required" lay-filter="treatmentTherapist"></select>
            <div class="ares-text-sm ares-text-red">
                <span id="therapist-append-group" class="ares-mr-lg"></span>
                <span id="therapist-append-patient-num"></span>
            </div>
        </div>
    </div>-->
    <div class="layui-form-item">
        <label class="layui-form-label">治疗项目</label>
        <div class="layui-input-block">
            <table class="layui-hide" id="treatment-item-table" lay-filter="table"></table>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submit">提交表单</button>
        </div>
    </div>
</form>


<script type="text/javascript" src="../../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['table', 'form', 'comboServices', 'mandyServices', 'qs', '_', 'utils'], function (table, form, comboServices, mandyServices, qs, _, utils) {
        var queryParams = qs.parse(location.search.substr(1)),
            editAble = !!(queryParams.edit * 1),
            cacheData = utils.ARES.cache.trainingProgramCombo,
            groupInfo = {},
//            therapists = [],
            comboList = [],
            trainingItems = [],
//            currentTherapist = {},
            currentCombo = {};
        console.log(cacheData)
        init();

        /**
         * @author: ares
         * @date: 2021/3/9 9:52
         * @description: 提交表单
         */
        form.on('submit(submit)', function (data) {
            var fields = data.field;
            filterCurrentCombo(fields.combo);
//            filterCurrentTherapist(fields.treatmentTherapist);
            if(!trainingItems.length){
                utils.msgError('没有有效的治疗项目');
                return false;
            }
            var info = $.extend({}, fields, {comboInfo: currentCombo, trainingItems: trainingItems});
            utils.ARES.event.emit('updateTrainingProgramCombo', info);
            utils.layer.closeAll('iframe');
            return false;
        });

        /**
         * @author: ares
         * @date: 2021/3/9 9:52
         * @description: 切换套餐
         */
        form.on('select(combo)', function (data) {
//            getTreatmentTherapist(data.value);
            getTrainingItem(filterCurrentCombo(data.value).id);
        });

        /**
         * @author: ares
         * @date: 2021/3/9 9:52
         * @description: 切换治疗师
         */
        /*form.on('select(treatmentTherapist)', function (data) {
            showTherapistAppendInfo();
        });*/

        /**
         * @author: ares
         * @date: 2021/3/10 16:20
         * @description: 根据套餐对应的治疗单类型id获取套餐信息
         * @param{string}  comboTypeId  餐对应的治疗单类型id
         */
        function filterCurrentCombo(comboTypeId) {
            var filterCombo = _.filter(comboList, function (combo) {
                return combo.typeId == comboTypeId;
            });
            if (filterCombo.length) {
                currentCombo = filterCombo[0];
                return filterCombo[0];
            }
            return null;
        }

        /**
         * @author: ares
         * @date: 2021/3/10 15:27
         * @description: 获取治疗师
         * @param{string} typeId  套餐对应的治疗单id
         */
        /*function getTreatmentTherapist(typeId) {
            mandyServices.mandy_get_therapist_by_typeId(typeId).then(function (res) {
                var data = res.data,
                    list = data.acc_list,
                    $treatmentTherapist = $('#treatment-therapist'),
                    $fragment = $(document.createDocumentFragment());
                therapists = list;
                groupInfo = data.group;
                $fragment.append('<option value="">请选择</option>');
                $.each(list, function (idx, item) {
                    if (!item.disable) $fragment.append('<option value="' + item.id + '">' + item.name + '</option>');
                });
                $treatmentTherapist.empty().append($fragment);
                if (editAble && $.isPlainObject(cacheData)) {
                    form.val('form', {
                        treatmentTherapist: cacheData.therapistInfo.id
                    });
                }
                form.val('form', {
                    treatmentTherapist: list[0] ? list[0].id : ''
                });
                form.render('select');
                showTherapistAppendInfo();
            });
        }*/

        /**
         * @author: ares
         * @date: 2021/3/10 16:20
         * @description: 根据治疗师id获取治疗师信息
         * @param{string}  therapistId  治疗师id
         */
        /*function filterCurrentTherapist(therapistId) {
            var filterTherapist = _.filter(therapists, function (therapist) {
                return therapist.id == therapistId;
            });
            if (filterTherapist.length) {
                currentTherapist = filterTherapist[0];
                return filterTherapist[0];
            }
            return null;
        }*/

        /**
         * @author: ares
         * @date: 2021/3/10 22:16
         * @description: 显示治疗师额外信息
         */
        /*function showTherapistAppendInfo() {
            var filterTherapist = filterCurrentTherapist(form.val('form').treatmentTherapist);
            if (!filterTherapist) return;
            $("#therapist-append-group").text('治疗师所在组: ' + groupInfo.name);
            $("#therapist-append-patient-num").text('治疗师负责患者人数: ' + filterTherapist.total_patient_number + '人');
        }*/

        /**
         * @author: ares
         * @date: 2021/3/10 15:27
         * @description: 获取套餐列表
         */
        function getCombo() {
            comboServices.getList(2).then(function (res) {
                var list = res.data.list,
                    $combo = $('#combo'),
                    $fragment = $(document.createDocumentFragment());
                comboList = list;
                $fragment.append('<option value="">请选择</option>')
                $.each(list, function (idx, item) {
                    $fragment.append('<option value="' + item.typeId + '">' + item.name + '</option>')
                });
                $combo.empty().append($fragment);
                if (editAble && $.isPlainObject(cacheData)) {
                    form.val('form', {
                        combo: cacheData.combo,
                        treatmentTherapist: cacheData.treatmentTherapist,
                    });
//                    getTreatmentTherapist(cacheData.comboInfo.typeId);
                    getTrainingItem(cacheData.comboInfo.id);
                }
//                getTreatmentTherapist(list[0].typeId);
                getTrainingItem(list[0].id);
                form.val('form', {
                    combo: list[0].typeId
                });
                form.render('select');
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/10 16:21
         * @description: 根据套餐id训练项目
         * @param{string} comboId 套餐id
         */
        function getTrainingItem(comboId) {
            comboServices.getComboItemService(comboId).then(function (res) {
                var data = res.data,
                    obj = data.list[0] || {},
                    list = [];
                $.each(obj, function (type, item) {
                    list = list.concat(item);
                })
                trainingItems = list;
                table.render({
                    elem: '#treatment-item-table',
                    data: list,
                    limit: list.length + 1,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '评定项目', field: 'itemName'},
                        {title: '编码', field: 'itemCode'},
                        {title: '单价(元)', field: 'unitFee'},
                    ]]
                });
                form.render();
            })
        }

        /**
         * @author: ares
         * @date: 2021/3/10 12:15
         * @description: 初始化
         */
        function init() {
            getCombo();
        }
    })
</script>
</body>

</html>
